<template>
	<view>
		<view class="tabs-view">
			<u-tabs :list="list1" @click="chooseTabs" lineColor="#008F3D"
			:activeStyle="{color: '#008F3D',fontWeight: 'bold',transform: 'scale(1.05)'}"></u-tabs>
		</view>
		<!-- 热销商品 -->
		<view class="flex justify-between" style="flex-wrap: wrap;margin:0 30rpx;">
			<block v-for="(item,index) in list" :key="index">
				<commonItem :item="item" :color="'#f25204'"></commonItem>
			</block>
		</view>
		<u-empty
		v-if="list.length==0"
		        mode="list"
		        icon="http://cdn.uviewui.com/uview/empty/list.png"
				text='暂无商品'
		>
		</u-empty>
		<view style="padding-bottom: 100rpx;">
			<u-loadmore v-if="loading" status="loading" />
			<u-loadmore v-if="isLastPage" status="nomore" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list1: [],
				id: '',
				page:1,
				limit:10,
				list:[],
				isLastPage: false,
				loading: false,
				classId:''
			}
		},
		onLoad(e) {
			this.id = e.id
			this.getCategoryReclassify(this.id)
		},
		onShow() {
			this.page = 1
			this.limit = 10
			this.list = []
			this.getClassifiedGoods(this.classId)
		},
		// 刷新 上拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.isLastPage = false
			this.list = []
			this.getClassifiedGoods(this.classId).finally(() => {
				// 停止当前页面下拉刷新
				uni.stopPullDownRefresh();
			});
		},
		// 加载下一页 下拉加载
		onReachBottom() {
			if (this.list.length >= this.page * this.limit) {
				this.isLastPage = false;
				this.page += 1;
				this.getClassifiedGoods(this.classId);
			} else {
				this.isLastPage = true
			}
		},
		methods: {
			// 获取二级分类
			getCategoryReclassify(id){
				this.$api.categoryReclassify({cate_id:id}).then(res=>{
					this.list1 = res.data
					this.classId = this.list1[0].id
					this.getClassifiedGoods(this.classId)
				})
			},
			// 获取二级分类商品
			getClassifiedGoods(id){
				let form = {
					cate_id:id,
					page:this.page,
					limit:this.limit
				}
				this.$api.classifiedGoods(form).then(res=>{
					console.log(res)
					if (res.code === 1) {
						 this.list = [...this.list, ...res.data]
					}
				})
			},
			// 选择tabs
			chooseTabs(e) {
				console.log(e.id)
				this.classId = e.id
				this.page = 1
				this.limit = 10
				this.list = []
				this.getClassifiedGoods( e.id)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabs-view {
		background-color: #ffffff;
		padding: 30rpx;
	}
</style>